/**
 * 十分彩规则(基类)
 */
var tenRule = function() {
    this.map = {

    };
    var arr = ['大', '小', '单', '双'];
    var arr2 = [1.99, 1.99, 1.988, 1.988];
    for (var i in arr) {
        this.map['和' + arr[i]] = {
            display: '总和 - ' + arr[i],
            rate: arr2[i],
        }
    }
    this.map['和尾大'] = {
        display: '总和 - 尾大',
        rate: 1.988,
    }
    this.map['和尾小'] = {
        display: '总和 - 尾小',
        rate: 1.988,
    }

    // 8球
    var arr = ['一', '二', '三', '四', '五', '六', '七', '八'];
    var arr2 = ['大', '小', '单', '双', '尾大', '尾小', '合单', '合双',     '东', '南', '西', '北', '中', '发', '白', '龙', '虎'];
    var arr3 = ['大', '小', '单', '双', '尾大', '尾小', '合数单', '合数双', '东', '南', '西', '北', '中', '发', '白', '龙', '虎'];
    var arr4 = [1.99, 1.99, 1.99, 1.99, 1.99, 1.99,    1.99,    1.99,    3.88,  3.88, 3.88, 3.88, 2.8, 2.8,  3.16, 1.99, 1.99];
    for (var i in arr) {
        for (var j in arr2) {
            if (i > 3 && j > 14) continue;
            this.map[arr[i] + arr2[j]] = {
                display: '第' + arr[i] + "球 - " + arr3[j],
                rate: arr4[j],
            }
        }
    }

    // 单号
    for (var i in arr) {
        this.map[arr[i]] = {
            display: '第' + arr[i] + "球",
            rate: 19.86,
        }
    }

    // 正码
    this.map["正"] = {
        display: '正码',
        rate: 2.46,
    }

    this.map["任2"] = {
        display: "任选2",
        rate: 6.5,
    };
    this.map["任3"] = {
        display: "任选3",
        rate: 19.6,
    };
    this.map["任4"] = {
        display: "任选4",
        rate: 60,
    };
    this.map["任5"] = {
        display: "任选5",
        rate: 250,
    };
    this.map["组2"] = {
        display: "选二连组",
        rate: 24,
    };
    this.map["组3"] = {
        display: "选三连组",
        rate: 830,
    };
};
tenRule.prototype = {};

tenRule.prototype.isOdd = function(v) {
    return v % 2 == 1;
};
tenRule.prototype.isEven = function(v) {
    return v % 2 == 0;
}
tenRule.prototype.isBig = function(v) {
    return v >= 11;
}
tenRule.prototype.isSmall = function(v) {
    return v < 11;
}
tenRule.prototype.isWeiDa = function(v) {
    return (v % 10) >= 5;
}
tenRule.prototype.isWeiXiao = function(v) {
    return (v % 10) < 5;
}
tenRule.prototype.isZhong = function(v) {
    var v = parseInt(v);
    return v >= 1 && v <= 7;
}
tenRule.prototype.isFa = function(v) {
    var v = parseInt(v);
    return v >= 8 && v <= 14;
}
tenRule.prototype.isBai = function(v) {
    var v = parseInt(v);
    return v >= 15 && v <= 20;
}
tenRule.prototype.idDong = function(v) {
    var v = parseInt(v);
    return v % 4 == 1;
}
tenRule.prototype.idNan = function(v) {
    var v = parseInt(v);
    return v % 4 == 2;
}
tenRule.prototype.idXi = function(v) {
    var v = parseInt(v);
    return v % 4 == 3;
}
tenRule.prototype.idBei = function(v) {
    var v = parseInt(v);
    return v % 4 == 0;
}

tenRule.prototype.isLong = function(list, index) {
    var a1 = parseInt(list[index]),
        a2 = parseInt(list[7 - index]);
    return a1 > a2;
}
tenRule.prototype.isHu = function(list, index) {
    var a1 = parseInt(list[index]),
        a2 = parseInt(list[7 - index]);
    return a1 < a2;
}

tenRule.prototype.isHeShuOdd = function(v) {
    var v = parseInt(v);
    var v = v % 10 + Math.floor(v / 10);
    return v % 2 == 1;
}

tenRule.prototype.isHeOdd = function(list) {
    var total = this.he(list);
    return total % 2 == 1;
}
tenRule.prototype.isHeBig = function(list) {
    var total = this.he(list);
    return total > 84;
}
tenRule.prototype.isHeSmall = function(list) {
    var total = this.he(list);
    return total < 84;
}

tenRule.prototype.isHeWeiBig = function(list) {
    var total = this.he(list);
    total = total % 10;
    return total >= 5;
}

// 计算总和
tenRule.prototype.he = function(list) {
    var total = 0;
    for (var i in list) {
        total += parseInt(list[i]);
    }
    return total;
}

/**
 * 计算露珠
 */
tenRule.prototype.luzhu = function(list, qiu, key) {
    // 将不是今天的开奖过滤掉
    var list2 = [];
    var today = new Date().Format("yyyy-MM-dd");
    for (var i in list) {
        if (list[i].time.startsWith(today)) list2.push(list[i]);
    }

    var self = this;
    if (key == 'count') {
        // 各数字的开奖次数
        var m = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
        for (var i in list2) {
            var n = parseInt(list[i].red[qiu - 1]);
            m[n - 1]++;
        }
        return m;
    }
    if (key == 'number') {
        // 号码排盘(最多25列)
        var m = [];
        for (var i in list2) {
            var n = parseInt(list[i].red[qiu - 1]);
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "daxiao") {
        // 大小
        var m = [];
        for (var i in list2) {
            var n = parseInt(list[i].red[qiu - 1]);
            var dx = self.isBig(n) ? "大" : "小";
            if (m.length == 0) {
                m.push({
                    n: dx,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == dx) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: dx,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "danshuang") {
        // 单双
        var m = [];
        for (var i in list2) {
            var n = parseInt(list[i].red[qiu - 1]);
            var dx = n % 2 == 0 ? "双" : "单";
            if (m.length == 0) {
                m.push({
                    n: dx,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == dx) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: dx,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "longhu") {
        // 龙虎
        if (qiu > 4) return [];
        var m = [];
        for (var i in list2) {
            var n = self.isLong(list2[i].red, qiu - 1) ? "龙" : "虎";
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "weishudaxiao") {
        // 尾数大小
        var m = [];
        for (var i in list2) {
            var n = parseInt(list2[i].red[qiu - 1]);
            n = self.isWeiDa(n) ? "大" : "小";
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "heshudanshuang") {
        // 合数单双
        var m = [];
        for (var i in list2) {
            var n = parseInt(list2[i].red[qiu - 1]);
            n = self.isHeShuOdd(n) ? "单" : "双";
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "hedaxiao") {
        // 总和大小
        var m = [];
        for (var i in list2) {
            var n = self.isHeBig(list2[i].red) ? "大" : "小";
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "hedanshuang") {
        // 总和单双
        var m = [];
        for (var i in list2) {
            var n = self.isHeOdd(list2[i].red) ? "单" : "双";
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
    if (key == "heweidaxiao") {
        // 总尾数大小
        var m = [];
        for (var i in list2) {
            var n = self.isHeWeiBig(list2[i].red) ? "大" : "小";
            if (m.length == 0) {
                m.push({
                    n: n,
                    c: 1,
                });
                continue;
            }

            var item = m[m.length - 1];
            if (item.n == n) item.c++;
            else {
                if (m.length >= 25) break;
                m.push({
                    n: n,
                    c: 1,
                });
            }
        }
        return m;
    }
}

/**
 * 计算长龙信息
 */
tenRule.prototype.long = function(list) {
    var self = this;
    var r = [];
    if (isEmpty(list)) return r;

    // 总和: 大、小、单、双，尾大 尾小
    var l0 = list[0].red;

    var f = function(k, cb) {
        var t = 1;
        for (var i = 1; i < list.length; i++) {
            var l = list[i].red;
            if (!cb(l)) break;
            t++;
        }
        var oo = {}; oo[k] = t;
        if (t > 2) r.push(oo);
    };

    if (self.isHeBig(l0)) {
        f("总和 - 大", function(l) {
            return self.isHeBig(l);
        });
    }
    else if (self.isHeSmall(l0)) {
        f("总和 - 小", function(l) {
            return self.isHeSmall(l);
        });
    }
    if (self.isHeOdd(l0)) {
        f("总和 - 单", function(l) {
            return self.isHeOdd(l);
        });
    }
    else {
        f("总和 - 双", function(l) {
            return !self.isHeOdd(l);
        });
    }
    if (self.isHeWeiBig(l0)) {
        f("总和尾大", function(l) {
            return self.isHeWeiBig(l);
        });
    }
    else {
        f("总和尾小", function(l) {
            return !self.isHeWeiBig(l);
        });
    }

    // 1-6球大小、单双
    var arr = ["第一球", "第二球", "第三球", "第四球", "第五球", "第六球", "第七球", "第八球"];
    for (var index = 0; index < 8; index++) {
        var n = parseInt(l0[index]);
        if (self.isBig(n)) {
            f(arr[index] + " - 大", function(l) {
                return self.isBig(parseInt(l[index]));
            });
        }
        else if (self.isSmall(n)) {
            f(arr[index] + " - 小", function(l) {
                return self.isSmall(parseInt(l[index]));
            });
        }
        if (self.isOdd(n)) {
            f(arr[index] + " - 单", function(l) {
                return self.isOdd(parseInt(l[index]));
            });
        }
        else if (self.isEven(n)) {
            f(arr[index] + " - 双", function(l) {
                return self.isEven(parseInt(l[index]));
            });
        }
        if (self.isWeiDa(n)) {
            f(arr[index] + " - 尾大", function(l) {
                return self.isWeiDa(parseInt(l[index]));
            });
        }
        else if (self.isWeiXiao(n)) {
            f(arr[index] + " - 尾小", function(l) {
                return self.isWeiXiao(parseInt(l[index]));
            });
        }
        if (self.isHeShuOdd(n)) {
            f(arr[index] + " - 合数单", function(l) {
                return self.isHeShuOdd(parseInt(l[index]));
            });
        }
        else {
            f(arr[index] + " - 合数双", function(l) {
                return !self.isHeShuOdd(parseInt(l[index]));
            });
        }
        if (index > 3) continue;
        if (self.isLong(l0, index)) {
            f(arr[index] + " - 龙", function(l) {
                return self.isLong(l, index);
            });
        }
        else {
            f(arr[index] + " - 虎", function(l) {
                return self.isHu(l, index);
            });
        }
    }

    // 排序
    r.sort(function(a, b) {
        var v1, v2;
        for (var k in a) v1 = a[k];
        for (var k in b) v2 = b[k];
        return v2 - v1;
    });
    return r;
}

/**
 * 转换显示的名称
 */
tenRule.prototype.getTargetName = function(name) {
    var arr = name.split(":");
    if (arr.length == 1)
        return this.map[name].display;
    return this.map[arr[0]].display + " &nbsp;<span class='blue' style='font-size:12px;'>" + arr[1] + "</span>";
};
